package com.team.sion.controller;

import com.alibaba.druid.util.StringUtils;
import com.team.sion.common.ErrorMessage;
import com.team.sion.common.SuccessMessage;
import com.team.sion.common.WarningMessage;
import com.team.sion.pojo.Consumer;
import com.team.sion.pojo.Singer;
import com.team.sion.pojo.Song;
import com.team.sion.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import com.team.sion.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class ConsumerController extends BaseController {
    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private HttpServletRequest req;

    @RequestMapping("")
    public Object userList() {
        List<Consumer> list = consumerService.findAll();
        return new SuccessMessage<List<Consumer>>("Consumer", list).getMessage();
    }

    // 登录
    @RequestMapping("/login/status")
    public Object login(@RequestParam String username,
                        @RequestParam String password) {
        Consumer consumer = new Consumer();
        consumer.setPassword(password);
        consumer.setUsername(username);
        List<Consumer> list = consumerService.findByUsernameAndPassword(consumer);
        if (list.size() != 0) {
            return new SuccessMessage<List<Consumer>>("登录成功", list).getMessage();
        } else {
            return new ErrorMessage("用户名或密码错误").getMessage();
        }
    }

    // 注册
    @RequestMapping("/add")
    public Object addUser(Consumer consumer) {
        boolean flag = consumerService.addUser(consumer);
        if (flag) {
            return new SuccessMessage<>("add").getMessage();
        }
        return new WarningMessage("failed").getMessage();
    }

    // 删除用户
    @RequestMapping("/delete")
    public Object deleteUser(Integer id) {
        boolean flag = consumerService.deleteUser(id);
        if (flag) {
            return new SuccessMessage<>("delete").getMessage();
        }
        return new WarningMessage("failed").getMessage();
    }

    @RequestMapping("/update")
    public Object updateUser(Consumer consumer) {
        boolean flag = consumerService.updateUser(consumer);
        if (flag) {
            return new SuccessMessage<>("update").getMessage();
        }
        return new WarningMessage("failed").getMessage();
    }

    @RequestMapping("/updatePassword")
    public Object updatePassword(Consumer consumer) {
        System.err.println(consumer);
        boolean flag = consumerService.updatePassword(consumer);
        if (flag) {
            return new SuccessMessage<>("update").getMessage();
        }
        return new WarningMessage("failed").getMessage();
    }

    @RequestMapping("/detail")
    public Object update(Integer id) {
        List<Consumer> list = consumerService.detail(id);
        return new SuccessMessage<List<Consumer>>("detail", list).getMessage();
    }

    @RequestMapping("/avatar/update")
    public Object testResponseJson(@RequestParam("file") MultipartFile avatorFile,
                                   @RequestParam("id") Integer id,
                                   HttpServletRequest request) throws Exception {
        // ⼀.定义⽂件名
        String fileName = "";
        // 1.获取原始⽂件名
        String uploadFileName = avatorFile.getOriginalFilename();
        // 2.截取⽂件扩展名
//        String extendName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
        // 3.把⽂件加上随机数，防⽌⽂件重复
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 4.判断是否输⼊了⽂件名
//        if (!StringUtils.isEmpty(""+id)) {
//            fileName = uuid + "_" + id + "." + extendName;
//        } else {
//            fileName = uuid + "_" + uploadFileName;
//        }
//        System.out.println(fileName);
        fileName = uuid + "_" + uploadFileName;
        // ⼆.获取⽂件路径
        ServletContext context = request.getServletContext();
        String basePath = context.getRealPath("/img/user");
        System.out.println(basePath);
        // 三.解决同⼀⽂件夹中⽂件过多问题
        String datePath = new SimpleDateFormat("yyyy-MM-dd")
                .format(new Date())
                .replaceAll("-", "");
        // 四.判断路径是否存在
        File file = new File(basePath + "/" + datePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String imgPic = "img/user" + "/" + datePath + "/" + fileName;
        consumerService.updatePic(id, imgPic);
        // 5.使⽤ MulitpartFile 接⼝中⽅法，把上传的⽂件写到指定位置
        avatorFile.transferTo(new File(file, fileName));
        return new SuccessMessage<String>("success", imgPic).getMessage();
    }
}
